package com.ctaa.ctaa.serviceimpl;

import com.ctaa.ctaa.dto.Result;
import com.ctaa.ctaa.mapper.UserMapper;
import com.ctaa.ctaa.pojo.UserInfoPojo;
import com.ctaa.ctaa.service.UserService;
import com.ctaa.ctaa.utils.MD5Utils;
import com.ctaa.ctaa.utils.QQMailSender;
import com.ctaa.ctaa.utils.StrongPasswordGenerator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
public class UserServiceImpl implements UserService {


    @Resource
    private UserMapper userMapper;

    @Override
    public Result<UserInfoPojo> login(String email, String password) {
        UserInfoPojo userInfoPojo = userMapper.queryUserInfoByEmail(email);
        if (userInfoPojo == null || userInfoPojo.getPassword() == null) {
            return Result.fail("邮箱不存在");
        }
        String password1 = userInfoPojo.getPassword();
        if (!password1.equalsIgnoreCase(password)) {
            return Result.fail("密码错误");
        }
        if (userInfoPojo.getId() > 0) {
            return Result.success("登陆成功", userInfoPojo);
        }
        return Result.fail("登陆失败,用户不存在");
    }

    @Override
    public Result<Boolean> regist(String userName, String password, String email) {
        UserInfoPojo userInfoPojo = userMapper.queryUserInfoByEmail(email);
        if (userInfoPojo != null && userInfoPojo.getId() > 0) {
            return Result.fail("邮箱存在");
        }
        String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Integer integer = userMapper.insertUserInfo(userName, password, email, format);
        if (integer > 0) {
            return Result.success("注册成功");
        }

        return Result.fail("注册失败");
    }

    /**
     * 重置密码
     *
     * @param email
     * @return
     */
    @Override
    public Result<Boolean> forgotPassword(String email) {

        UserInfoPojo userInfoPojo = userMapper.queryUserInfoByEmail(email);
        if (userInfoPojo == null || userInfoPojo.getPassword() == null) {
            return Result.fail("邮箱不存在");
        }
        String s = StrongPasswordGenerator.generatePassword();//新密码
        Boolean aBoolean = QQMailSender.sendEmail(email, s);
        String encrypt = MD5Utils.encrypt(s);
        userMapper.updatePassword(encrypt, email);
        if (aBoolean) {
            return Result.success("邮件发送成功");
        }
        return Result.success("邮件发送失败");
    }

    @Override
    public Result<Boolean> modifyPass(String email, String password) {
        UserInfoPojo userInfoPojo = userMapper.queryUserInfoByEmail(email);
        String password1 = userInfoPojo.getPassword();
        if (password1.equalsIgnoreCase(password)) {
            return Result.fail("新密码与原密码相同");
        }

        Integer integer = userMapper.updatePassword(password, email);
        if (integer > 0) {
            return Result.success("密码修改成功");
        }
        return Result.fail("密码修改失败");
    }
}
